/* eslint-disable no-nested-ternary */
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
// 阿里云图标配置//at.alicdn.com/t/c/font_3501871_xfc3obqe2.js
export const iconfontVersion = ['3501871_xfc3obqe2']; // iconfont更新
export const iconfontUrl = `//at.alicdn.com/t/c/font_$key.js`;
// 文件类型图片
export const fileImg = {
  xlsx: require('@/assets/img/files/icon_excel_48px@2x.png'),
  xls: require('@/assets/img/files/icon_excel_48px@2x.png'),
  doc: require('@/assets/img/files/icon_word_48px@2x.png'),
  docx: require('@/assets/img/files/icon_word_48px@2x.png'),
  ppt: require('@/assets/img/files/icon_ppt_48px@2x.png'),
  pptx: require('@/assets/img/files/icon_ppt_48px@2x.png'),
  png: require('@/assets/img/files/icon_img_48px@2x.png'),
  jpg: require('@/assets/img/files/icon_img_48px@2x.png'),
  jpeg: require('@/assets/img/files/icon_img_48px@2x.png'),
  pdf: require('@/assets/img/files/icon_pdf_48px@2x.png'),
  mp4: require('@/assets/img/files/icon_shipin_24px@2x.png'),
  mp3: require('@/assets/img/files/icon_music_40px@2x.png'),
  txt: require('@/assets/img/files/icon_txt_24px@2x.png'),
  other: require('@/assets/img/files/icon_wenjian_48px@2x.png')
};
// 审批状态
export const statusObj = {
  0: '未审核',
  1: '已通过',
  2: '审核中',
  3: '已撤销',
  4: '已拒绝',
  5: '已转交',
  null: ''
};
export const statusShow = {
  0: '',
  1: require('@/assets/img/mx-icon-tongguo.png'),
  2: require('@/assets/img/mx-icon-zu3902.png'),
  3: require('@/assets/img/mx-icon-jujue1.png'),
  4: require('@/assets/img/mx-icon-jujue1.png'),
  5: require('@/assets/img/mx-icon-tongguo.png')
};
export const ImgObj = {
  0: require('@/assets/img/oa_img_w_shenhe@2x.png'),
  1: require('@/assets/img/oa_img_tongguo@2x.png'),
  2: require('@/assets/img/oa_img_shenhe@2x.png'),
  3: require('@/assets/img/xiangqing_icon_yijieshu_nor@2x.png'),
  4: require('@/assets/img/oa_img_jujue@2x.png'),
  5: require('@/assets/img/oa_img_tongguo@2x.png')
};
/**
 * num 小于0，左缩进num*2个空格； 大于0，右缩进num*2个空格。
 * @param {string} str 代码
 * @param {number} num 缩进次数
 * @param {number} len 【可选】缩进单位，空格数
 */
export function indent(str, num, len = 2) {
  if (num === 0) return str;
  const isLeft = num < 0;
  const result = [];
  let reg;
  let spaces = '';
  if (isLeft) {
    num *= -1;
    reg = new RegExp(`(^\\s{0,${num * len}})`, 'g');
  } else {
    for (let i = 0; i < num * len; i++) spaces += ' ';
  }

  str.split('\n').forEach(line => {
    line = isLeft ? line.replace(reg, '') : spaces + line;
    result.push(line);
  });
  return result.join('\n');
}

// 首字母大小
export function titleCase(str) {
  return str.replace(/( |^)[a-z]/g, L => L.toUpperCase());
}

// 下划转驼峰
export function camelCase(str) {
  return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase());
}

export function isNumberStr(str) {
  return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str);
}

export const exportDefault = 'export default ';

export const beautifierConf = {
  html: {
    indent_size: '2',
    indent_char: ' ',
    max_preserve_newlines: '-1',
    preserve_newlines: false,
    keep_array_indentation: false,
    break_chained_methods: false,
    indent_scripts: 'separate',
    brace_style: 'end-expand',
    space_before_conditional: true,
    unescape_strings: false,
    jslint_happy: false,
    end_with_newline: true,
    wrap_line_length: '110',
    indent_inner_html: true,
    comma_first: false,
    e4x: true,
    indent_empty_lines: true
  },
  js: {
    indent_size: '2',
    indent_char: ' ',
    max_preserve_newlines: '-1',
    preserve_newlines: false,
    keep_array_indentation: false,
    break_chained_methods: false,
    indent_scripts: 'normal',
    brace_style: 'end-expand',
    space_before_conditional: true,
    unescape_strings: false,
    jslint_happy: true,
    end_with_newline: true,
    wrap_line_length: '110',
    indent_inner_html: true,
    comma_first: false,
    e4x: true,
    indent_empty_lines: true
  }
};

function stringify(obj) {
  return JSON.stringify(obj, (key, val) => {
    if (typeof val === 'function') {
      return `${val}`;
    }
    return val;
  });
}

function parse(str) {
  JSON.parse(str, (k, v) => {
    if (v.indexOf && v.indexOf('function') > -1) {
      // eslint-disable-next-line no-eval
      return eval(`(${v})`);
    }
    return v;
  });
}

export function jsonClone(obj) {
  return parse(stringify(obj));
}

// 深拷贝对象
export function deepClone(obj) {
  const _toString = Object.prototype.toString;

  // null, undefined, non-object, function
  if (!obj || typeof obj !== 'object') {
    return obj;
  }

  // DOM Node
  if (obj.nodeType && 'cloneNode' in obj) {
    return obj.cloneNode(true);
  }

  // Date
  if (_toString.call(obj) === '[object Date]') {
    return new Date(obj.getTime());
  }

  // RegExp
  if (_toString.call(obj) === '[object RegExp]') {
    const flags = [];
    if (obj.global) {
      flags.push('g');
    }
    if (obj.multiline) {
      flags.push('m');
    }
    if (obj.ignoreCase) {
      flags.push('i');
    }

    return new RegExp(obj.source, flags.join(''));
  }

  const result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {};

  for (const key in obj) {
    result[key] = deepClone(obj[key]);
  }

  return result;
}

const toStr = Function.prototype.call.bind(Object.prototype.toString);
export function isObjectObject(t) {
  return toStr(t) === '[object Object]';
}
export function isObjectArray(t) {
  return toStr(t) === '[object Array]';
}
export function isObjectNull(t) {
  return toStr(t) === '[object Null]';
}
export function isObjectUnde(t) {
  return toStr(t) === '[object Undefined]';
}
export const mix = (color1, color2, weight) => {
  weight = Math.max(Math.min(Number(weight), 1), 0);
  let r1 = parseInt(color1.substring(1, 3), 16);
  let g1 = parseInt(color1.substring(3, 5), 16);
  let b1 = parseInt(color1.substring(5, 7), 16);
  let r2 = parseInt(color2.substring(1, 3), 16);
  let g2 = parseInt(color2.substring(3, 5), 16);
  let b2 = parseInt(color2.substring(5, 7), 16);
  let r = Math.round(r1 * (1 - weight) + r2 * weight);
  let g = Math.round(g1 * (1 - weight) + g2 * weight);
  let b = Math.round(b1 * (1 - weight) + b2 * weight);
  r = ('0' + (r || 0).toString(16)).slice(-2);
  g = ('0' + (g || 0).toString(16)).slice(-2);
  b = ('0' + (b || 0).toString(16)).slice(-2);
  return '#' + r + g + b;
};
export function hexToGgb(val) {
  let sColor = val.toLowerCase();
  // 十六进制颜色值的正则表达式
  let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  // 如果是16进制颜色
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      let sColorNew = '#';
      for (let i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
      }
      sColor = sColorNew;
    }
    // 处理六位的颜色值
    let sColorChange = [];
    for (let i = 1; i < 7; i += 2) {
      sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)));
    }
    return sColorChange.join(',');
  }
  return sColor;
}
